<?php
include("../config.php");
require_once("../clases/cResultado.php");
require_once("../clases/PHPLogger.php");
$LOGGER = new PHPLogger(LOGGER_PATH, LOGGER_LEVEL);
class ServerRequest
{	
	function getResultado($mysqli) {
		if ($rs = $mysqli->store_result()) {
       		if($rs->num_rows >0) {
    			$i=0;
				while ($finfo = $rs->fetch_field()) {
              		$campos[$finfo->name]=$i;
              	$i++;
				}
				while ($row = $rs->fetch_row()) $vec[] = $row;
					$rs->free_result();
				return new cResultado($campos, $vec);
       		}
			$mysqli->close();
		}
		return new cResultado();
	}

	/**********************************************************************/
	function getEventoById($id, $tableEvento) // Dado un ID retorna un evento.
	{
	 	global $LOGGER;
		if (($id=='')||($id==null)){
			return (0);
		}
		$mysqli = new mysqli(HOST_bykom, USER_bykom, PASSWORD_bykom, DB_bykom); 
	    if (mysqli_connect_errno()) {
	        return (0);
    	}			 	 	 
	    $SQL = "SELECT * FROM $tableEvento Ev ";
		//$SQL = "SELECT * FROM evmatemporal_prueba Ev ";
		$SQL.= "INNER JOIN almacodigos as ALM on ALM.`ORDER_ID` = Ev.EVENTO ";
		$SQL.= "WHERE Ev.ORDER_ID = $id";
	 	if (!$mysqli->real_query($SQL)) {
	 		$LOGGER->addLine('ERROR', $mysqli->error.' CONSULTA: '.$SQL);
     		return (0);
     	}		
	 	$resultado = ServerRequest::getResultado($mysqli);
	 	$mysqli->close();
     	return ($resultado);
    }			
	
	/**********************************************************************/
	function getCuentaById($id) // Dado un ID retorna una cuenta.
	{
	 	global $LOGGER;
		if (($id=='')||($id==null)){
			return (0);
		}
		$mysqli = new mysqli(HOST_bykom, USER_bykom, PASSWORD_bykom, DB_bykom); 
	    if (mysqli_connect_errno()) {
	        return (0);
    	}			 	 	 

	    $SQL = "select *, ";
        $SQL.= "TipoAvl.`NOMBRE` as tipoAvl, ";
        $SQL.= "if((isNull(PosiActual.`LATITUD`) or PosiActual.`LATITUD`=''),ABMA.`LATITUD`,PosiActual.`LATITUD`)as LAST_LAT, ";
        $SQL.= "if((isNull(PosiActual.`LONGITUD`) or PosiActual.`LATITUD`=''),ABMA.`LONGITUD`,PosiActual.`LONGITUD`)as LAST_LON, ";
        $SQL.= "calles.`nombre` as DIR_calle, ";
        $SQL.= "CalleTipo.`NOMBRE` as DIR_calleTipo, ";
        $SQL.= "CalleEntre.nombre as DIR_entre1, ";
        $SQL.= "CalleTipo1.`NOMBRE` as DIR_entreTipo1, ";
        $SQL.= "CalleY.nombre as DIR_entre2, ";
        $SQL.= "CalleTipo2.`NOMBRE` as DIR_entreTipo2, ";
        $SQL.= "cast(ABMA.`NUMERO` as char) as DIR_numero, ";
        $SQL.= "ABMA.`PISO` as DIR_piso, ";
        $SQL.= "ABMA.`DPTO` as DIR_dpto, ";
        $SQL.= "Barrio.nombre as DIR_barrio, ";
        $SQL.= "CiudadX.nombre as DIR_ciudad, ";
        $SQL.= "CiudadX.provincia as DIR_provincia, ";
        $SQL.= "CiudadX.pais as DIR_pais, ";
        $SQL.= "ABMA.`CODPOSTAL1` as DIR_cp1, ";
        $SQL.= "cast(ABMA.`codpostal2`as char) as DIR_cp2, ";
        $SQL.= "ABMA.`CODPOSTAL3` as DIR_cp3, ";
        $SQL.= "cast(CiudadX.caratelefo as char) as DIR_caracterisitcaTel, ";
		$SQL.= "CRC.`CODIGOALFA` as codAlfa, ";
		$SQL.= "PosiActual.`APROXIMACION` as DIR_movil, ";
		$SQL.= "PosiActual.`FECHAHORA` as DIR_lastFecha ";
		$SQL.= "from abmacodigos ABMA ";
		$SQL.= "LEFT JOIN `abrltipoavl` as TipoAvl on ABMA.`AVL_TIPO` = TipoAvl.`ORDER_ID` ";
		$SQL.= "LEFT JOIN `posi_actual` as PosiActual on ABMA.`ORDER_ID` = PosiActual.`ORDER_RL` ";
		$SQL.= "LEFT JOIN calles on ABMA.`CALLE`=calles.`order_id` ";
		$SQL.= "LEFT JOIN `calles_tipos` CalleTipo on CalleTipo.`ORDER_ID` = calles.`order_id` ";
		$SQL.= "LEFT JOIN calles as CalleEntre on CalleEntre.order_id = ABMA.`CALLE_ENT1` ";
		$SQL.= "LEFT JOIN `calles_tipos` CalleTipo1 on CalleTipo1.`ORDER_ID` = CalleEntre.`order_id` ";
		$SQL.= "LEFT JOIN calles as CalleY on CalleY.order_id = ABMA.`CALLE_ENT2` ";
		$SQL.= "LEFT JOIN `calles_tipos` CalleTipo2 on CalleTipo2.`ORDER_ID` = CalleY.`order_id` ";
		$SQL.= "LEFT JOIN barrios as Barrio on ABMA.codigobarr = Barrio.codigobarr ";
		$SQL.= "LEFT JOIN ciudad as CiudadX on ABMA.`CODIGOCIUD`=CiudadX.codigociud ";
		$SQL.= "INNER JOIN rcmacodigos as CRC on CRC.`ORDER_ID` = ABMA.id_rc ";
		$SQL.= "where ABMA.`ORDER_ID` = $id ";

	 	if (!$mysqli->real_query($SQL)) {
	 		$LOGGER->addLine('ERROR', $mysqli->error.' CONSULTA: '.$SQL);
     		return (0);
     	}		
	 	$resultado = ServerRequest::getResultado($mysqli);
	 	$mysqli->close();
     	return ($resultado);
    }	
} 
?>
